import { notification } from 'ant-design-vue';
import { getAppRouter } from '~/modules/router/helpers';
import { TtNotifyType } from './NotificationItem.vue';

interface ICommonType {
  type: TtNotifyType;
  id: number;
}

interface IReadNotifyType {
  url?: string;
  linkType?: number;
  orderNo?: string;
  callback?: () => void;
  isPopup?: boolean;
}

interface INotifyType extends ICommonType {
  title: string;
  message: string;
  readNotify: IReadNotifyType;
}

export const noticeMap = reactive(new Map<string, INotifyType>());

// notification.config({
//   maxCount: 2,
// });

function joinNotifyId(id: number, type: TtNotifyType) {
  return `${type}_${id}`;
}

function setNotify(id: number, title: string, message: string, type: TtNotifyType, readNotify: IReadNotifyType) {
  const key = joinNotifyId(id, type);
  noticeMap.set(key, {
    id,
    title,
    message,
    type,
    readNotify,
  });
}

function showNotify() {
  const bp = useAppBreakpoints();
  if (noticeMap.size === 0 || bp.ltMobile)
    return;
  const key = noticeMap.keys().next().value!;
  const _notifyData = noticeMap.get(key)!;
  notify({
    id: _notifyData.id,
    title: _notifyData.title,
    message: _notifyData.message,
    type: _notifyData.type,
    readNotify: _notifyData.readNotify,
  });
}

/**
 *
 * @param params
 * @param params.type
 * @param params.id
 * @param params.url
 * @param params.linkType // 链接类型 1 充值记录 2 提币记录 3 Help center 4.帮助-隐私 5 帮助-公平   6帮助-支持 // 7 帮助-费用  8 帮助-谷歌  9 帮助-faq  10 帮助-货币  11 帮助-注册  12 帮助-主页 16 注册送
 * @param params.orderNo
 * @param params.callback
 * @param params.isPopup
 */
async function readNotify<T extends ICommonType & IReadNotifyType>(params: T) {
  const router = getAppRouter();
  if (params.type === TtNotifyType.PLATEFORM) {
    if (params?.url) {
      if (params?.url?.startsWith('http'))
        window.open(params.url, '_blank');
      else
        router.push(params.url);
    }
    await readPlatformNotify(params.id);
  }

  if (params.type === TtNotifyType.USER) {
    if (params.linkType) {
      // 链接类型 1 充值记录 2 提币记录 3 Help center 4.帮助-隐私 5 帮助-公平   6帮助-支持
      // 7 帮助-费用  8 帮助-谷歌  9 帮助-faq  10 帮助-货币  11 帮助-注册  12 帮助-主页
      switch (params.linkType) {
        case 1:
          router.push({ name: RN.USER_TRANSATIONS, query: { active: 0 } });
          break;
        case 2:
          router.push({ name: RN.USER_TRANSATIONS, query: { active: 1 } });
          break;
        case 3:
          //
          break;
        case 4:
          router.push({ name: RN.HELP_PRIVACY_POLICY });
          break;
        case 5:
          router.push({ name: RN.HELP_FAIRNESS });
          break;
        case 6:
          router.push({ name: RN.HELP_SUPPORT });
          break;
        case 7:
          router.push({ name: RN.HELP_FEE });
          break;
        case 8:
          router.push({ name: RN.HELP_GOOGLE_AUTHENTICATOR });
          break;
        case 9:
          router.push({ name: RN.HELP_FAQS });
          break;
        case 10:
          router.push({ name: RN.HELP_CURRENCY });
          break;
        case 11:
          router.push({ name: RN.HELP_REGISTRATIONANDLOGIN });
          break;
        case 12:
          router.push({ name: RN.REFERRAL });
          break;
        case 13:
          globalPopups.rankPopup.open();
          break;
        case 14:
          router.push({ name: RN.USER_TRANSATIONS, query: { active: 7, orderNo: params.orderNo } });
          break;
        case 15:
          router.push({ name: RN.USER_TRANSATIONS, query: { active: 5 } });
          break;
        case 16:
          // eslint-disable-next-line style/max-statements-per-line
          setTimeout(() => { globalPopups.wallet.open({ initTab: 'deposit', isDepositPage: true }); }, 500);
          break;
        default :
          return false;
          break;
      }
    }
    await readUserNotify(params.id);
  }
  if (params.callback)
    params.callback();
  if (params.isPopup)
    return;
  closeNoticeById(params.id, params.type);
}

function closeNoticeById(id: number, type: TtNotifyType) {
  notification.close(id.toString());
  const notifyId = joinNotifyId(id, type);
  noticeMap.delete(notifyId);
  Object.values(globalPopups).forEach(c => c.close());
  showNotify();
}

/**
 * 标记平台提示为已阅读
 * @param id
 */
async function readPlatformNotify(id?: number) {
  return await apis.apiNoticePlatformReadAll({ notice_id: id ?? 0 })
    .then(() => {
      queryClient.invalidateQueries({
        queryKey: [apis.apiUserInfo.id],
        exact: false,
      });
    })
    .catch((e) => {
      antdMessage.warning(e);
    });
}

/**
 * 标记用户提示为已阅读
 * @param id
 */
async function readUserNotify(id: number) {
  return await apis.apiNoticePersonalRead({ notice_id: id ?? 0 })
    .then(() => {
      queryClient.invalidateQueries({
        queryKey: [apis.apiUserInfo.id],
        exact: false,
      });
    })
    .catch((e) => {
      antdMessage.warning(e);
    });
}

/**
 * 通知
 */
function notify(params: INotifyType) {
  // 弹出通知弹窗
  notification.open({
    class: 'tt-notification',
    message: '',
    duration: 0,
    key: params.id.toString(),
    description: () => h(defineAsyncComponent(() => import('./NotificationItem.vue')), {
      title: params.title,
      message: params.message,
      type: params.type,
    }, undefined),
    onClick() {
      readNotify({ type: params.type, id: params.id, ...params.readNotify });
    },
    onClose() {
      readNotify({ type: params.type, id: params.id });
    },
  });
}

export {
  closeNoticeById,
  notify,
  readNotify,
  setNotify,
  showNotify,
};
